const path = require("path");
const webpack = require("webpack");

/*
 * SplitChunksPlugin is enabled by default and replaced
 * deprecated CommonsChunkPlugin. It automatically identifies modules which
 * should be splitted of chunk by heuristics using module duplication count and
 * module category (i. e. node_modules). And splits the chunks…
 *
 * It is safe to remove "splitChunks" from the generated configuration
 * and was added as an educational example.
 *
 * https://webpack.js.org/plugins/split-chunks-plugin/
 *
 */

/*
 * We've enabled TerserPlugin for you! This minifies your app
 * in order to load faster and run less javascript.
 *
 * https://github.com/webpack-contrib/terser-webpack-plugin
 *
 */

const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
  mode: "production",
  entry: "./source/index.js",

  output: {
    path: path.resolve(__dirname, "src"),
    filename: "index.js",
    library: 'add',
    libraryTarget: 'umd',
    libraryExport: 'default',
    scriptType: 'module'
  },

  plugins: [new webpack.ProgressPlugin()],

  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        include: [path.resolve(__dirname, "source")],
        loader: "babel-loader",
      },
    ],
  },

  optimization: { minimize: true,
    minimizer: [
      new TerserPlugin({
        // exclude: /index.js/,
        parallel: true,
        terserOptions: {
          ecma:6,
          format: {
            comments: false,
          },

          keep_classnames: true,
          keep_fnames: true,
          module: true,
          toplevel: true,
          compress: {
            // keep_fargs: true,
            defaults: false,
            // arrows: true,
            // dead_code: false,
            // directives: false,
            // drop_debugger: true,
            drop_console: true,
            // ecma: true,
            // expression: true,
            // side_effects: false,
            // keep_classnames: true,
            // keep_fnames: true,
            module: true,
            // // top_retain:true,
            // unused: "keep_assign",
            // reduce_vars
          },
        },
        extractComments: false,
      }),
    ],

    splitChunks: {
      cacheGroups: {
        vendors: {
          priority: -10,
          test: /[\\/]node_modules[\\/]/,
        },
      },

      chunks: "async",
      minChunks: 1,
      minSize: 30000,
      name: false,
    },
  },
};
